/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.jh.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hcht.xrlh.jh.entity.DrJh;
import com.hcht.xrlh.jh.service.DrJhService;
import com.hcht.xrlh.sx.entity.DrSx;
import com.hcht.xrlh.sx.service.DrSxService;
import com.hcht.xrlh.zfdxk.entity.DrZfdxk;
import com.hcht.xrlh.zfdxk.service.DrZfdxkService;
import com.hcht.xrlh.zfryk.entity.DrZfryk;
import com.hcht.xrlh.zfryk.service.DrZfrykService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 双随机计划Controller
 * @author lrz
 * @version 2016-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/jh/drJh")
public class DrJhController extends BaseController {

	@Autowired
	private DrJhService drJhService;
	
	@Autowired
	private DrSxService drSxService;
	
	@Autowired
	private DrZfrykService drZfrykService;
	
	@Autowired
	private DrZfdxkService drZfdxkService;
	
	@ModelAttribute
	public DrJh get(@RequestParam(required=false) String id) {
		DrJh entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drJhService.get(id);
		}
		if (entity == null){
			entity = new DrJh();
		}
		return entity;
	}
	
	@RequiresPermissions("jh:drJh:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrJh drJh, HttpServletRequest request, HttpServletResponse response, Model model) {
/*		if(drJh.getOfficeZd()==null){
			drJh.setOfficeZd(UserUtils.getUser().getOffice());
		}*/
		for(Role role : UserUtils.getUser().getRoleList()){
			if("1".equals(role.getId())){
			}else{
				drJh.setOfficeZd(UserUtils.getUser().getOffice());
			}
		}
		/*drJh.setOfficeZd(UserUtils.getUser().getOffice());*/
		Page<DrJh> page = drJhService.findPage(new Page<DrJh>(request, response), drJh); 
		model.addAttribute("page", page);
		return "xrlh/jh/drJhList";
	}

	@RequiresPermissions("jh:drJh:view")
	@RequestMapping(value = "form")
	public String form(DrJh drJh, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("sxId"); 
		drJh.setSxId(sxId);
		DrSx sx = drSxService.get(sxId);
		drJh.setJhFdsx(sx.getSxFdsx());
		
		DrZfryk ry = new DrZfryk();
		ry.setSxId(sxId);
		int rykNum = drZfrykService.findList(ry).size();
		
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		int dxkNum = drZfdxkService.findList(dx).size();
		
		model.addAttribute("rykNum", rykNum);
		model.addAttribute("dxkNum", dxkNum);
		model.addAttribute("drJh", drJh);
		model.addAttribute("sxMc", sx.getSxMc());
		model.addAttribute("sx", sx);
		return "xrlh/jh/drJhForm";
	}

	@RequestMapping(value = "form_view")
	public String form_view(DrJh drJh, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("sxId"); 
		drJh.setSxId(sxId);
		DrSx sx = drSxService.get(sxId);
		drJh.setJhFdsx(sx.getSxFdsx());
		
		DrZfryk ry = new DrZfryk();
		ry.setSxId(sxId);
		int rykNum = drZfrykService.findList(ry).size();
		
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		int dxkNum = drZfdxkService.findList(dx).size();
		
		model.addAttribute("rykNum", rykNum);
		model.addAttribute("dxkNum", dxkNum);
		model.addAttribute("drJh", drJh);
		model.addAttribute("sxMc", sx.getSxMc());
		return "xrlh/jh/drJhForm_view";
	}
	
	@RequiresPermissions("jh:drJh:view")
	@RequestMapping(value = {"listSx", ""})
	public String listSx(DrSx drSx, HttpServletRequest request, HttpServletResponse response, Model model) {
		DrSx sx = new DrSx();
		sx.setOfficeZd(UserUtils.getUser().getOffice());
		//drSx.setCreateBy(UserUtils.getUser());
		sx.setpIds(UserUtils.getUser().getOffice().getParentIds());
		sx.setuFlag("0");
		Page<DrSx> page = drSxService.findPage2(new Page<DrSx>(request, response), sx); 
		model.addAttribute("page", page);
		return "xrlh/jh/drJcxxForm";
	}
	
	@RequiresPermissions("jh:drJh:edit")
	@RequestMapping(value = "save")
	public String save(DrJh drJh, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		if (!beanValidator(model, drJh)){
			return form(drJh, model,request,response);
		}
		String methodFlag = request.getParameter("methodflag");
		drJhService.save(drJh);
		addMessage(redirectAttributes, "保存双随机计划成功");
		if("1".equals(methodFlag)){
			redirectAttributes.addAttribute("sxId", drJh.getSxId());
			redirectAttributes.addAttribute("jhId", drJh.getId());
			return "redirect:"+Global.getAdminPath()+"/rw/drRw/form";
		}else{
			return "redirect:"+Global.getAdminPath()+"/jh/drJh/list?repage";
		}
	}
	
	@RequiresPermissions("jh:drJh:edit")
	@RequestMapping(value = "delete")
	public String delete(DrJh drJh, RedirectAttributes redirectAttributes) {
		drJhService.delete(drJh);
		addMessage(redirectAttributes, "删除双随机计划成功");
		return "redirect:"+Global.getAdminPath()+"/jh/drJh/list?repage";
	}
}